package com.whmall.service.crm.module.companyusererrorcorrection.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.companyusererrorcorrection.CorrectionDTO;
import com.whmall.service.crm.entity.dto.companyusererrorcorrection.CorrectionListDTO;
import com.whmall.service.crm.module.company.dao.CompanyMapper;
import com.whmall.service.crm.module.company.entity.CompanyDO;
import com.whmall.service.crm.module.company.entity.qo.CompanyQO;
import com.whmall.service.crm.module.companyuser.dao.CompanyUserMapper;
import com.whmall.service.crm.module.companyuser.entity.CompanyUserDO;
import com.whmall.service.crm.module.companyuser.entity.qo.CompanyUserQO;
import com.whmall.service.crm.module.companyusererrorcorrection.dao.CompanyUserErrorCorrectionMapper;
import com.whmall.service.crm.module.companyusererrorcorrection.entry.CompanyUserErrorCorrectionDO;
import com.whmall.service.crm.module.companyusererrorcorrection.entry.qo.CorrectionQO;
import com.whmall.service.crm.module.companyusererrorcorrection.service.CompanyUserErrorCorrectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qiao shengda
 * @since 2021/5/27
 */
@Service
public class CompanyUserErrorCorrectionServiceImpl implements CompanyUserErrorCorrectionService {

    private CompanyUserErrorCorrectionMapper companyUserErrorCorrectionMapper;
    private CompanyMapper companyMapper;
    private CompanyUserMapper companyUserMapper;

    @Override
    public PageRspDTO<CorrectionListDTO> page(PageReqDTO pageReqDTO, CorrectionQO correctionQO) {
        // 分页
        PageHelper.startPage(pageReqDTO.getPageNum(), pageReqDTO.getPageSize());
        List<CompanyUserErrorCorrectionDO> corrections = companyUserErrorCorrectionMapper.selectList(correctionQO);
        PageInfo<CompanyUserErrorCorrectionDO> pageInfo = new PageInfo<>(corrections);
        // 初始化
        List<CompanyUserErrorCorrectionDO> list = pageInfo.getList();
        // 空值判断
        if(list.isEmpty()){
            return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), (int) pageInfo.getTotal(), new ArrayList<>(0));
        }
        Map<Long, CompanyDO> companyS = companyMapper.listBy(CompanyQO.builder()
                .ids(list.stream().map(CompanyUserErrorCorrectionDO::getCompanyId).distinct().collect(Collectors.toList()))
                .build()).stream().collect(Collectors.toMap(CompanyDO::getId, e -> e));
        Map<Long, CompanyUserDO> users = companyUserMapper.listBy(CompanyUserQO.builder()
                .ids(list.stream().map(CompanyUserErrorCorrectionDO::getUserId).distinct().collect(Collectors.toList()))
                .build()).stream().collect(Collectors.toMap(CompanyUserDO::getId, e -> e));
        // 遍历赋值
        List<CorrectionListDTO> temps = list.stream().map(item -> {
            CorrectionListDTO temp = new CorrectionListDTO();
            // 查询公司
            CompanyDO company = companyS.get(item.getCompanyId());
            // 查询用户
            CompanyUserDO user = users.get(item.getUserId());
            // 赋值
            BeanUtil.copyProperties(item, temp);
            if (ObjectUtil.isNotNull(company)) {
                temp.setCompanyName(company.getName());
            }
            if (ObjectUtil.isNotNull(user)) {
                temp.setUserEmail(user.getEmail());
                temp.setUserName(user.getName());
            }
            return temp;
        }).collect(Collectors.toList());
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), (int) pageInfo.getTotal(), temps);
    }

    @Override
    public int response(Long id, String content) {
        CompanyUserErrorCorrectionDO correction = new CompanyUserErrorCorrectionDO();
        correction.setId(id);
        correction.setIsResponse(1);
        correction.setResponseContent(content);
        correction.setResponseTime(new Date());
        return companyUserErrorCorrectionMapper.updateByIdSelective(correction);
    }

    @Override
    public PageRspDTO<CorrectionDTO> pageByUserId(PageReqDTO pageReqDTO, Long userId) {
        // 分页
        PageHelper.startPage(pageReqDTO.getPageNum(), pageReqDTO.getPageSize());
        List<CompanyUserErrorCorrectionDO> corrections = companyUserErrorCorrectionMapper.selectAllByUserId(userId);
        PageInfo<CompanyUserErrorCorrectionDO> pageInfo = new PageInfo<>(corrections);
        // 空值判断
        if (pageInfo.getList().isEmpty()){
            return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), (int) pageInfo.getTotal(), new ArrayList<>(0));
        }
        // 赋值
        List<CorrectionDTO> collect = pageInfo.getList().stream().map(item -> BeanUtil.copyProperties(item, CorrectionDTO.class)).collect(Collectors.toList());
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), (int) pageInfo.getTotal(), collect);
    }

    @Override
    public int save(Long userId, Long chemicalId, String content) {
        CompanyUserErrorCorrectionDO correction = new CompanyUserErrorCorrectionDO();
        CompanyUserDO user = companyUserMapper.getById(userId);
        correction.setCompanyId(user.getCompanyId());
        correction.setUserId(userId);
        correction.setChemicalId(chemicalId);
        correction.setErrorCorrectionContent(content);
        correction.setErrorCorrectionTime(new Date());
        return companyUserErrorCorrectionMapper.insertSelective(correction);
    }

    @Override
    public int remove(Long id, Long userId) {
        CompanyUserErrorCorrectionDO correction = new CompanyUserErrorCorrectionDO();
        correction.setId(id);
        correction.setIsResponse(0);
        correction.setResponseContent("");
        correction.setResponseTime(DateUtil.parseDateTime("1972-01-01 00:00:00"));
        return companyUserErrorCorrectionMapper.updateByIdSelective(correction);
    }

    @Autowired
    public CompanyUserErrorCorrectionServiceImpl(CompanyUserErrorCorrectionMapper companyUserErrorCorrectionMapper, CompanyMapper companyMapper, CompanyUserMapper companyUserMapper) {
        this.companyUserErrorCorrectionMapper = companyUserErrorCorrectionMapper;
        this.companyMapper = companyMapper;
        this.companyUserMapper = companyUserMapper;
    }
}
